using System;
using NUnit.Framework;
using Rhino.Mocks;
using Sedge.Core.Configuration;
using Sedge.Core.Configuration.Report;

namespace Sedge.Tests.Configuration
{
    [TestFixture]
    public class SuiteTest
    {
        [Test]
        public void SelectApplicationTest()
        {
            Suite suite = new Suite();
            suite.Applications.AddRange(new[]
                                        {
                                            new ReportableObject {Name = "1", Caption = "1"},
                                            new ReportableObject {Name = "2", Caption = "2"},
                                            new ReportableObject {Name = "3", Caption = "3"}
                                        });
            suite.SelectApplication("2");

            Assert.IsTrue(suite.SelectedApplication.Caption == "2");
        }

        [Test]
        public void MergeSuitePropertiesTest()
        {
            Suite suite = new Suite();
            suite.Properties.Add("1", "11");
            suite.Properties.Add("2", "22");
            suite.Properties.Add("10", "1010");

            ReportableObject obj = new ReportableObject();
            obj.Properties.Add("5", "55");
            obj.Properties.Add("6", "66");
            obj.Properties.Add("11", "11");
            obj.Properties.Add("2", "222");

            suite.SelectedApplication = obj;
            suite.MergeSuiteWithSelectedApp();

            Assert.IsTrue(suite.Properties.Count == 6);
            Assert.IsTrue(suite.Properties.ContainsKey("11"));
            Assert.IsTrue(suite.Properties["2"] == "222");
        }

        private Group CreateGroup(string id)
        {
            Group group1 = new Group { Name = "g" + id, Caption = "g" + id };
            Page page1 = new Page() { Name = "p" + id, Caption = "p" + id };
            for (int i = 0; i < 3; i++)
            {
                ISource source = MockRepository.GenerateStub<ISource>();
                source.Caption = Convert.ToString(i);
                page1.Sources.Add(source);
            }
            group1.Pages.Add(page1);
            return group1;
        }

        [Test]
        public void MergeGroupsTest()
        {
            Suite suite = new Suite();

            suite.Groups.Add(CreateGroup("1"));

            ReportableObject obj = new ReportableObject();
            Group g = CreateGroup("1");
            g.Pages[0].Sources.Add(MockRepository.GenerateStub<ISource>());
            obj.Groups.Add(g);
            obj.Groups.Add(CreateGroup("2"));

            suite.SelectedApplication = obj;
            suite.MergeSuiteWithSelectedApp();

            Assert.IsTrue(suite.Groups.Count == 2);
            Assert.IsTrue(suite.Groups[0].Pages.Count == 1);
            Assert.IsTrue(suite.Groups[0].Pages[0].Sources.Count == 7);
        }

    }
}